package com.lc.pianostudent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.pianostudent.R;
import com.lc.pianostudent.bean.DegreeBean;
import com.lc.pianostudent.bean.PianoRecordBean;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class DegreeAdapter extends RecyclerView.Adapter<DegreeAdapter.ViewHolder>{


    private Context context;

    private List<DegreeBean> list = new ArrayList<>();

    public DegreeAdapter(Context context, List<DegreeBean> list) {
        this.context = context;
        this.list = list;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    private DegreeAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(DegreeAdapter.OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_degree, parent, false);

        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) view);

        DegreeAdapter.ViewHolder viewHolder = new DegreeAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.e("=====",list.get(position).getHead()+"===");
        holder.fenshu.setText(list.get(position).getNumber()+"-"+list.get(position).getHead());

        holder.nianji.setText(list.get(position).getTitle());


        if(position == list.size()-1){
            holder.fenshu.setBackgroundResource(R.drawable.layer_white_solid_gray_left_bottom_right_20px);
            holder.nianji.setBackgroundResource(R.drawable.layer_white_solid_gray_bottom_right_20px);
        }else {
            holder.fenshu.setBackgroundResource(R.drawable.layer_white_solid_gray_left_bottom_right);
            holder.nianji.setBackgroundResource(R.drawable.layer_white_solid_gray_bottom_right);
        }


        setUpItemEvent(holder,position);

    }

    //为RecyclerView添加点击事件
    protected void setUpItemEvent(final DegreeAdapter.ViewHolder holder, final  int position) {

        if (onItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //  int LayoutPosition = holder.getLayoutPosition();

                    onItemClickListener.onItemClick(holder.itemView, position);



                }

            });

        }

    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    //初始化组件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView fenshu,nianji;


        public ViewHolder(View itemView) {
            super(itemView);

            fenshu = (TextView) itemView.findViewById(R.id.fenshu);
            nianji = (TextView) itemView.findViewById(R.id.nianji);

        }
    }
}
